package com.lc.user.express.view;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.lc.user.express.R;
import com.lc.user.express.app.AppContext;
import com.lc.user.express.view.wheel.ArrayWheelAdapter;
import com.lc.user.express.view.wheel.OnWheelScrollListener;
import com.lc.user.express.view.wheel.WheelView;

import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * Created by Administrator on 2016/03/15. 时间选择控件
 */
public class DatePopup extends PopupWindow {

    protected String[] dates;//日期
    protected String[] hours;//小时
    protected String[] minutes;//分钟
    private WheelView wdate;//日期
    private WheelView whour;//小时
    private WheelView wminute;//分钟
    private Calendar c;
    private int hour;

    private View view = null;

    private Context context;

    private OnClickOk onClickOk;

    public DatePopup(Context context, int w, int h) {
        super(context);
        c = Calendar.getInstance();
        hour = c.get(Calendar.HOUR_OF_DAY);

        this.context = context;
        this.setContentView(getDataPick(context));
        this.setWidth(w);
        this.setHeight(h);
        this.setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0x80000000);
        this.setBackgroundDrawable(dw);
    }

    private View getDataPick(Context context) {
        view = LayoutInflater.from(context).inflate(R.layout.wheel_date_picker, null);
        AppContext.ScaleScreenHelper.loadView((ViewGroup) view);

        wdate = (WheelView) view.findViewById(R.id.year);
        getDate(5);//默认5天
        wdate.addScrollingListener(scrollListener);
        wdate.setWheelBackground(R.color.background);

        whour = (WheelView) view.findViewById(R.id.month);
        getHours(24 - hour);//小时，默认当日剩余小时数
        whour.setCyclic(false);
        whour.addScrollingListener(scrollListener);
        whour.setWheelBackground(R.color.background);

        wminute = (WheelView) view.findViewById(R.id.day);
        getMinutes(4, false);//分钟，默认4个时间
        wminute.setCyclic(false);
        wminute.setWheelBackground(R.color.background);
        wminute.addScrollingListener(scrollListener);

        wdate.setVisibleItems(5);//设置显示行数
        whour.setVisibleItems(5);
        wminute.setVisibleItems(5);

        TextView tv_ok = (TextView) view.findViewById(R.id.tv_ok);
        TextView tv_no = (TextView) view.findViewById(R.id.tv_no);
        tv_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (onClickOk != null) {
                    onClickOk.onClickOk(dates[wdate.getCurrentItem()],hours[whour.getCurrentItem()],minutes[wminute.getCurrentItem()],wdate.getCurrentItem());
                    dismiss();
                }
            }
        });
        tv_no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        view.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = view.findViewById(R.id.ll_container).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });
        return view;
    }

    String temp = "今天";
    OnWheelScrollListener scrollListener = new OnWheelScrollListener() {
        @Override
        public void onScrollingStarted(WheelView wheel) {

        }

        @Override
        public void onScrollingFinished(WheelView wheel) {

            if (wdate.getCurrentItem() < dates.length && "今天".equals(dates[wdate.getCurrentItem()])) {
                hour = c.get(Calendar.HOUR_OF_DAY);
                getHours(24 - hour);
                if (!temp.equals(dates[wdate.getCurrentItem()])) whour.setCurrentItem(0);
                if (whour.getCurrentItem() < hours.length && "立即用车".equals(hours[whour.getCurrentItem()])) {
                    getMinutes(4, false);
                } else {
                    getMinutes(4, true);
                }

            } else {
                hour = 0;
                getHours(24);
                getMinutes(4, true);
            }
            temp = dates[wdate.getCurrentItem()];

        }
    };

    /**
     * @return
     * 获取今天开始的5天日历天数
     */
    private void getDate(int count) {
        dates = new String[5];
        SimpleDateFormat format = new SimpleDateFormat("MM月dd日");
        Calendar c = Calendar.getInstance();
        dates[0] = "今天";
        dates[1] = "明天";
        int today = c.get(Calendar.DAY_OF_MONTH);
        int month = c.get(Calendar.MONTH);
        int year = c.get(Calendar.YEAR);
        for (int i = 2; i < count; i++) {
            int temp = c.get(Calendar.DAY_OF_MONTH) + i;
            c.set(Calendar.DAY_OF_MONTH, temp);
            dates[i] = format.format(c.getTime());
            c.set(Calendar.DAY_OF_MONTH, today);
            c.set(Calendar.MONTH, month);
            c.set(Calendar.YEAR, year);
        }
        ArrayWheelAdapter<String> dateAdapter = new ArrayWheelAdapter<String>(context, dates);
        wdate.setViewAdapter(dateAdapter);
    }

    /**
     * @param temp 24：24小时，其他情况为今天剩余小时数
     */
    private void getHours(int temp) {

        hours = new String[temp];
        if (temp == 24)  {   //24小时
            for (int i = 0; i < temp; i++) {
                if (i < 10) {
                    hours[i] = "0" + i + "时";
                } else {
                    hours[i] = i + "时";
                }
            }
            ArrayWheelAdapter<String> hourAdapter = new ArrayWheelAdapter<String>(context, hours);
            whour.setViewAdapter(hourAdapter);
            return;
        }

        hours[0] = "立即用车";//今天剩余小时数
        for (int i = 1; i < temp; i++) {
            if ((hour + i) < 10) {
                hours[i] = "0" + (hour + i) + "时";
            } else {
                hours[i] = (hour + i) + "时";
            }
        }
        ArrayWheelAdapter<String> hourAdapter = new ArrayWheelAdapter<String>(context, hours);
        whour.setViewAdapter(hourAdapter);
    }

    /**
     * @param t
     * @param is
     * 分钟 00 15 30 45
     */
    private void getMinutes(int t, boolean is) {
        minutes = new String[t];
        if (is) {
            minutes[0] = "00分";
            minutes[1] = "15分";
            minutes[2] = "30分";
            minutes[3] = "45分";
        } else {
            minutes[0] = "";
            minutes[1] = "";
            minutes[2] = "";
            minutes[3] = "";
        }

        ArrayWheelAdapter<String> numericWheelAdapter = new ArrayWheelAdapter<String>(context, minutes);
        wminute.setViewAdapter(numericWheelAdapter);
    }

    public interface OnClickOk {
        void onClickOk(String date, String hour, String minute, int position);
    }

    public void setOnClickOk(OnClickOk onClickOk) {
        this.onClickOk = onClickOk;
    }
}

